package com.incar.chuxing.payAndShare;

import android.app.Activity;
import android.content.Context;

import com.incar.chuxing.bean.WXPreBean;
import com.incar.chuxing.utils.ToastUtils;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * Created by michal on 2016/10/11.
 */
public class WXPayUtils {

    private Context mContext;
    public static String app_id;

    public WXPayUtils(Context mContext) {
        this.mContext = mContext;
    }

    public boolean wxPay(final WXPreBean.AttachmentBean.BeanBean orderInfo) {
        final IWXAPI wxapi = WXAPIFactory.createWXAPI(mContext, null);
        app_id = orderInfo.getAppId();
        wxapi.registerApp(app_id);
        if (!wxapi.isWXAppInstalled()) {
            ((Activity) mContext).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ToastUtils.makeText(mContext, "请您先安装微信", ToastUtils.LENGTH_SHORT).show();
                }
            });
            return false;
        }
        PayReq req = new PayReq();
        req.appId = app_id;
        req.partnerId = orderInfo.getPartnerId();
        req.sign = orderInfo.getSign();
        req.prepayId = orderInfo.getPrepayid();
        req.timeStamp = orderInfo.getTimestamp();
        req.packageValue = orderInfo.getPackage_();
        req.nonceStr = orderInfo.getNonceStr();
        wxapi.sendReq(req);
        return true;
    }
}
